# -*- conding:UTF-8 -*-

'''
@ Project: P7_lx1
@ File: jwtToken_generate_check.py
@ Author: 罗婷
@ Date：2022/6/8 19:11

'''
import datetime

import jwt

from flask import current_app

# TODO 报错记录 应用上下文获取配置数据 current_app.config['名称']
# 生成token
def generate_token(payload, algorithm='HS256'):
    secret = current_app.config.get('SECRET_KEY')
    # secret = current_app.config['SECRET_KEY']
    exprie = datetime.datetime.now() + datetime.timedelta(hours=24)
    _payload = {'exp': exprie}
    _payload.update(payload)
    token = jwt.encode(_payload, secret, algorithm)
    print('这是生成的token', token)
    return token

# 校验token
def check_token(token, algorithm='HS256'):
    secret = current_app.config.get('SECRET_KEY')
    try:
        payload = jwt.decode(token, secret, algorithm)
        return payload
    except:
        return None





























